home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3 / CHAPTER3 / CUSTOMIZ.C < prev    next >
C/C++ Source or Header  |  1996-03-06  |  11KB  |  335 lines

  1.  
  2. #include <windows.h>  
  3. #include <commctrl.h>
  4. #include "customiz.h"  
  5.  
  6.  
  7.  
  8. #if defined (WIN32)
  9.     #define IS_WIN32 TRUE
  10. #else
  11.     #define IS_WIN32 FALSE
  12. #endif
  13.  
  14. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  15. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  16. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  17.  
  18. HINSTANCE hInst;   // current instance
  19.  
  20. LPCTSTR lpszAppName = "MyApp";
  21. LPCTSTR lpszTitle   = "TB_CUSTOMIZE"; 
  22.  
  23.  
  24. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  25.  
  26.  
  27. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  28.                       LPTSTR lpCmdLine, int nCmdShow)
  29. {
  30.    MSG      msg;
  31.    HWND     hWnd; 
  32.    WNDCLASS wc;
  33.  
  34.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  35.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  36.    wc.cbClsExtra    = 0;                      
  37.    wc.cbWndExtra    = 0;                      
  38.    wc.hInstance     = hInstance;              
  39.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  40.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  41.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  42.    wc.lpszMenuName  = lpszAppName;              
  43.    wc.lpszClassName = lpszAppName;              
  44.  
  45.    if ( IS_WIN95 )
  46.    {
  47.       if ( !RegisterWin95( &wc ) )
  48.          return( FALSE );
  49.    }
  50.    else if ( !RegisterClass( &wc ) )
  51.       return( FALSE );
  52.  
  53.    hInst = hInstance; 
  54.  
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107. #define IDC_STATBAR   101
  108. #define IDC_TOOLBAR   102
  109.  
  110. // Toolbar buttons. 
  111. //.................
  112. TBBUTTON tbButtons[] = 
  113.     { STD_FILENEW,  IDM_NEW, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0}, 
  114.     { STD_FILEOPEN, IDM_OPEN, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0}, 
  115.     { STD_FILESAVE, IDM_SAVE, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0}, 
  116.     { VIEW_LARGEICONS, IDM_LARGEICON, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0}, 
  117.     { VIEW_SMALLICONS, IDM_SMALLICON, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0}, 
  118.     { VIEW_LIST, IDM_LISTVIEW, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0}, 
  119.     { VIEW_DETAILS, IDM_REPORTVIEW, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0}, 
  120. };
  121.  
  122.  
  123. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  124. {
  125. static char szTemp[128];
  126. static HWND hToolWnd   = NULL;
  127. static HWND hStatusWnd = NULL;
  128. static BOOL bNeedSave  = FALSE;
  129.  
  130.    switch( uMsg )
  131.    {
  132.       case WM_CREATE :
  133.               // Initialize the common control library
  134.               // and create the status window and toolbar.
  135.               //..........................................
  136.               InitCommonControls();
  137.               hStatusWnd = CreateStatusWindow( 
  138.                                   WS_CHILD | WS_VISIBLE | 
  139.                                   CCS_BOTTOM | SBARS_SIZEGRIP, 
  140.                                   "", hWnd, IDC_STATBAR );
  141.  
  142.               hToolWnd = CreateToolbarEx( hWnd, 
  143.                                           WS_CHILD | WS_BORDER | WS_VISIBLE | CCS_ADJUSTABLE, 
  144.                                           IDC_TOOLBAR, 
  145.                                           11,
  146.                                           (HINSTANCE)HINST_COMMCTRL, 
  147.                                           IDB_STD_SMALL_COLOR, 
  148.                                           tbButtons, 3,
  149.                                           0, 0, 100, 30, sizeof(TBBUTTON) );
  150.  
  151.               if ( hToolWnd )
  152.               {
  153.                  TBADDBITMAP  tb;
  154.                  TBSAVEPARAMS tbsp;
  155.                  int  index, stdidx;
  156.  
  157.                  // Add the system-defined view bitmaps.
  158.                  //.....................................
  159.                  tb.hInst = HINST_COMMCTRL;
  160.                  tb.nID = IDB_VIEW_SMALL_COLOR;
  161.                  stdidx = SendMessage(hToolWnd, TB_ADDBITMAP, 12, (LPARAM)&tb);
  162.  
  163.                  // Update the indices to the bitmaps.
  164.                  //...................................
  165.                  for (index = 3; index < 7; index++)
  166.                     tbButtons[index].iBitmap += stdidx;
  167.  
  168.                  // Restore the toolbar settings.
  169.                  //..............................
  170.                  tbsp.hkr          = HKEY_CURRENT_USER;
  171.                  tbsp.pszSubKey    = "Software\\Waite\\TB_CUSTOMIZE";
  172.                  tbsp.pszValueName = "Toolbar";
  173.  
  174.                  SendMessage(hToolWnd, TB_SAVERESTORE, FALSE, (LPARAM)&tbsp);
  175.               }
  176.               break;
  177.  
  178.       case WM_SIZE :
  179.               {
  180.                  RECT clrect, strect;
  181.  
  182.                  // Size the status window when the main
  183.                  // window is sized.
  184.                  //.....................................
  185.                  GetClientRect( hWnd, &clrect );
  186.                  GetClientRect( hStatusWnd, &strect );
  187.  
  188.                  MoveWindow( hStatusWnd, 0, clrect.bottom-strect.bottom, 
  189.                              LOWORD( lParam ), HIWORD( lParam ), TRUE );
  190.               }
  191.               break;
  192.  
  193.       case WM_NOTIFY :
  194.               if ( ((LPNMHDR)lParam)->idFrom == IDC_TOOLBAR )
  195.               {
  196.                  LPTBNOTIFY lpTBNotify = (LPTBNOTIFY)lParam;
  197.  
  198.                  switch( lpTBNotify->hdr.code )
  199.                  {
  200.                     case TBN_QUERYINSERT : 
  201.                          return( TRUE );
  202.  
  203.                     case TBN_QUERYDELETE :
  204.                          return( TRUE );
  205.  
  206.                     case TBN_GETBUTTONINFO :
  207.                          if ( lpTBNotify->iItem < 7 )
  208.                          {
  209.                             lpTBNotify->tbButton = tbButtons[lpTBNotify->iItem];
  210.                           
  211.                             // Load the description for the button.
  212.                             //.....................................
  213.                             if ( lpTBNotify->pszText )
  214.                             {
  215.                                LoadString( hInst, tbButtons[lpTBNotify->iItem].idCommand,
  216.                                            szTemp, sizeof( szTemp )-1 );
  217.  
  218.                                strcpy( lpTBNotify->pszText, szTemp );
  219.                                lpTBNotify->cchText = strlen( szTemp );
  220.                             }
  221.                             return( TRUE ); 
  222.                          }
  223.                          break;
  224.  
  225.                     case TBN_RESET : // Reset the toolbar.
  226.                          {
  227.                             int nButtons, i;
  228.  
  229.                             nButtons = SendMessage(hToolWnd, TB_BUTTONCOUNT, 0, 0);
  230.                             for( i=0; i < nButtons; i++ )
  231.                                SendMessage(hToolWnd, TB_DELETEBUTTON, 0, 0);
  232.  
  233.                             SendMessage(hToolWnd, TB_ADDBUTTONS, 3, (LPARAM)tbButtons);
  234.                          }
  235.                          break;
  236.  
  237.                     case TBN_CUSTHELP : // Display custom help file.
  238.  
  239.                          WinHelp( hWnd, "CUSTOMIZ.HLP", HELP_CONTEXT, 1 );
  240.                          break;
  241.  
  242.                     case TBN_BEGINADJUST :
  243.                          SendMessage(hStatusWnd, SB_SETTEXT, 0, 
  244.                                      (LPARAM)"Customizing the toolbar." );
  245.                          break;
  246.  
  247.                     case TBN_BEGINDRAG :
  248.                          SendMessage(hStatusWnd, SB_SETTEXT, 0, 
  249.                                      (LPARAM)"Dragging a toolbar button." );
  250.                          break;
  251.  
  252.                     case TBN_ENDDRAG :
  253.                     case TBN_ENDADJUST :
  254.                          SendMessage(hStatusWnd, SB_SETTEXT, 0, 
  255.                                      (LPARAM)"" );
  256.                          break;
  257.  
  258.                     case TBN_TOOLBARCHANGE :
  259.                          SendMessage(hStatusWnd, SB_SETTEXT, 0, 
  260.                                  (LPARAM)"The toolbar has changed." );
  261.  
  262.                          bNeedSave = TRUE;
  263.                          break;
  264.                  }
  265.               }
  266.               break;
  267.  
  268.       case WM_COMMAND :
  269.               switch( LOWORD( wParam ) )
  270.               {
  271.                  case IDM_CUSTOMIZE :
  272.                         SendMessage(hToolWnd, TB_CUSTOMIZE, 0, 0);
  273.                         break;
  274.  
  275.                  case IDM_ABOUT :
  276.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  277.                         break;
  278.  
  279.                  case IDM_EXIT :
  280.                         DestroyWindow( hWnd );
  281.                         break;
  282.               }
  283.               break;
  284.       
  285.       case WM_DESTROY :
  286.               // Save the toolbar settings.
  287.               //...........................
  288.               if ( bNeedSave )
  289.               {
  290.                  TBSAVEPARAMS tbsp;
  291.  
  292.                  tbsp.hkr          = HKEY_CURRENT_USER;
  293.                  tbsp.pszSubKey    = "Software\\Waite\\TB_CUSTOMIZE";
  294.                  tbsp.pszValueName = "Toolbar";
  295.  
  296.                  SendMessage(hToolWnd, TB_SAVERESTORE, TRUE, (LPARAM)&tbsp );
  297.               }
  298.  
  299.               WinHelp( hWnd, "CUSTOMIZ.HLP", HELP_QUIT, 0 );
  300.  
  301.               PostQuitMessage(0);
  302.               break;
  303.  
  304.       default :
  305.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  306.    }
  307.  
  308.    return( 0L );
  309. }
  310.  
  311.  
  312. LRESULT CALLBACK About( HWND hDlg,           
  313.                         UINT message,        
  314.                         WPARAM wParam,       
  315.                         LPARAM lParam)
  316. {
  317.    switch (message) 
  318.    {
  319.        case WM_INITDIALOG: 
  320.                return (TRUE);
  321.  
  322.        case WM_COMMAND:                              
  323.                if (   LOWORD(wParam) == IDOK         
  324.                    || LOWORD(wParam) == IDCANCEL)    
  325.                {
  326.                        EndDialog(hDlg, TRUE);        
  327.                        return (TRUE);
  328.                }
  329.                break;
  330.    }
  331.  
  332.    return (FALSE); 
  333. }
  334.